package com.tanhua.manage.controller;

import com.tanhua.manage.service.UserFreezeService;
import com.tanhua.manage.service.UserUnfreezeService;
import com.tanhua.manage.service.UsersManageService;
import com.tanhua.manage.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage")
@Slf4j
public class UsersManageController {

    @Autowired
    private UsersManageService usersManageService;

    @Autowired
    private UserUnfreezeService userUnfreezeService;

    @Autowired
    private UserFreezeService userFreezeService;


    /**
     * 分页查询用户数据
     *
     * @param id       用户id
     * @param nickname 昵称
     * @param city     城市
     * @param page     页码
     * @param pagesize 页面大小
     * @return UserVo[]
     */
    @PostMapping("users")
    public ResponseEntity<PageInfo<UserVo>> queryByPage(@RequestParam(value = "id", required = false) String id,
                                                        @RequestParam(value = "nickname", required = false) String nickname,
                                                        @RequestParam(value = "city", required = false) String city,
                                                        @RequestParam(value = "page", defaultValue = "1", required = false) Long page,
                                                        @RequestParam(value = "pagesize", defaultValue = "20", required = false) Long pagesize) {

        try {
            PageInfo<UserVo> userVoPageInfo = usersManageService.queryByPage( page, pagesize);
            if (null != userVoPageInfo) {
                return ResponseEntity.ok(userVoPageInfo);
            }
        } catch (Exception e) {
            log.error("查询用户基本资料出错~", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * 查询用户基本资料
     *
     * @return
     */
    @GetMapping("users/{userID}")
    public ResponseEntity<UsersManageVo> queryUsersById(@PathVariable("userID") Long userId) {
        try {
            UsersManageVo usersManageVo = usersManageService.queryUsersById(userId);
            if (null != usersManageVo) {
                return ResponseEntity.ok(usersManageVo);
            }
        } catch (Exception e) {
            log.error("查询用户基本资料出错~ userId = " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 用户冻结操作
     *
     * @return
     */
    @PostMapping("users/freeze")
    public ResponseEntity<UsersFreezeVo> UserFreeze(@RequestBody Map<String, String> param) {
        try {
            Long userId = Long.valueOf(param.get("userId"));//用户id
            Integer freezingTime = Integer.valueOf(param.get("freezingTime"));//冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
            Integer freezingRange = Integer.valueOf(param.get("freezingRange"));//冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
            String reasonsForFreezing = param.get("reasonsForFreezing");//冻结原因
            String frozenRemarks = param.get("frozenRemarks");//冻结备注
            UsersFreezeVo usersFreezeVo = userFreezeService.UserFreeze(userId, freezingTime
                    , freezingRange, reasonsForFreezing, frozenRemarks);
            if (null != usersFreezeVo) {
                return ResponseEntity.ok(usersFreezeVo);
            }
        } catch (Exception e) {
            log.error("用户冻结失败 ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * 用户解冻
     *
     * @return
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity<UsersFreezeVo> UserUnFreeze(@RequestBody Map<String, String> param) {
        try {
            Long userId = Long.valueOf(param.get("userId"));//用户id
            String reasonsForThawing = param.get("reasonsForThawing");//解冻原因
            UsersFreezeVo usersFreezeVo = userUnfreezeService.UserUnFreeze(userId, reasonsForThawing);
            if (null != usersFreezeVo) {
                return ResponseEntity.ok(usersFreezeVo);
            }
        } catch (Exception e) {
            log.error("用户解冻出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
